/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewEmp.java
 *
 * Created on Jul 16, 2011, 10:35:58 PM
 */
package javadatabasegui;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Qin
 */
public class ViewEmp extends javax.swing.JFrame {
    Connection con;
    String userEmail;
    String username;
    /** Creates new form ViewEmp */
    public ViewEmp(String email, String uName, Connection conn) {
        userEmail = email;
        con = conn;
        username = uName;
        initComponents();
        displayCerts();
    }

    public ViewEmp( String email, Connection conn){
        userEmail = email;
        con = conn;
        initComponents();
        displayCerts();
    }

    private ViewEmp() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    
   // private String getUserName(){
   //     String userName = "Name";
        //use userEmail to get the F_Name and L_Name
   //     try{
   //        Statement stmt = (Statement) con.createStatement();
   //         ResultSet rs = null;
   //         if (stmt.execute("SELECT F_Name,L_Name FROM User WHERE Email=\"" + userEmail + "\"")){
   //             rs = stmt.getResultSet();
   //         }
   //         rs.next();
   //         userName = rs.getString("F_Name") + " " + rs.getString("L_Name");

   //     }catch(SQLException e){
   //         System.err.println(e.getMessage());
   //         System.err.println(e.getSQLState());
   //     }
        
   //     return userName;
   // }

    private String UserName(){
       String dashName = "Name";
        //use userEmail to get the F_Name and L_Name
        try{
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            if (stmt.execute("SELECT F_Name,L_Name FROM Users WHERE Email=\""
                    + userEmail +"\"")){
                rs = stmt.getResultSet();
            }
            rs.next();
            dashName = rs.getString("F_Name") + " " + rs.getString("L_Name");
        }catch(SQLException e){
            System.err.println(e.getMessage());
            System.err.println(e.getSQLState());
        }
        return dashName;
    }
    
    private String UserEmail(){
      String uEmail = userEmail;
      return uEmail;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        username_ve = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        useremail_ve = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        username_ve.setFont(new java.awt.Font("Tahoma", 1, 18));
        username_ve.setText(UserName());

        jLabel2.setText("Email");

        useremail_ve.setText(UserEmail());

        jPanel2.setBackground(new java.awt.Color(218, 239, 244));

        jLabel4.setText("Certifications");

        jScrollPane1.setBackground(new java.awt.Color(218, 239, 244));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 586, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 175, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jPanel3);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(442, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jButton1.setText("Close");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(useremail_ve, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(username_ve, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(username_ve, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(useremail_ve, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(11, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
                //new EmployeeManagement(con).setVisible(true);
                this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel useremail_ve;
    private javax.swing.JLabel username_ve;
    // End of variables declaration//GEN-END:variables


    private void displayCerts() {
        int numEntries = 0;
        
        try{
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            if (stmt.execute("SELECT COUNT(Certifications) from `cs4400_Group_14`.`User_Cert` WHERE U_Email = \"" + userEmail +"\"")){
                rs = stmt.getResultSet();
            }
            rs.next();
            numEntries = rs.getInt("COUNT(Certifications)");
        }catch(SQLException e){
            System.err.println("Error in Project Count");
            System.err.println(e.getMessage());
            }
        
        jPanel3.setPreferredSize(new java.awt.Dimension(570, numEntries*25));


        javax.swing.JLabel[] tstLabel = new javax.swing.JLabel[numEntries];
        
        for(int i=0; i<tstLabel.length; i++){
            tstLabel[i] = new javax.swing.JLabel();
        }

        try{
            Statement stmt = con.createStatement();
            ResultSet rs = null;
            if (stmt.execute("SELECT Certifications from `cs4400_Group_14`.`User_Cert` WHERE U_Email = \"" + userEmail +"\"")){
                rs = stmt.getResultSet();
            }
         for(int i=0; rs.next();i++){           
            tstLabel[i].setText("<html> <body> <table width=\"560\"> <tr> <td width=\"100%\">" + rs.getString("Certifications") + "</td>"
                    + "</tr> </table> </body> </html>");         
            }           
         
        }catch(SQLException e){
            System.err.println("Error in Projects Display");
            System.err.println(e.getMessage());
            }
        
        jPanel3.setLayout(new GridBagLayout());
        GridBagConstraints cnst1 = new GridBagConstraints();
        for(int i=0; i<tstLabel.length; i++){
            tstLabel[i].setFont(new Font("Tahoma",4,14));
            cnst1.gridx = 0;
            cnst1.gridy = i;
            jPanel3.add(tstLabel[i],cnst1);
        }
        cnst1.gridy = tstLabel.length + 1;
        cnst1.gridx = 0;
        cnst1.weighty = (numEntries) * 25;
        jPanel3.add(new javax.swing.JLabel(), cnst1);
    }
}
